﻿using Dos.ORM;
using System.Data;
using orderCreater.Model.WSY;
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace orderCreater
{
    public class WSY_CreaterModel
    {
        //MySql 数据库操作对象
        public DbSession db_w;
        public WSY_CreaterModel()
        {
            db_w = new DbSession(DatabaseType.MySql, "Data Source=rm-bp1niu13oy19th27o.mysql.rds.aliyuncs.com;Database=web531;User Id=web531;Password=web53$$$%%#@;Convert Zero Datetime=True;Allow Zero Datetime=True;");
            var where20161226 = db_w.From<Db_goods>().Count();
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public string AddOrder(Db_order order)
        {
            string reVal = "0";
            db_w.Insert<Db_order>(order);
            reVal = db_w.Max<Db_order>(Db_order._.order_id, Db_order._.order_id != null).ToString();
            return reVal;
        }
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public int AddOrderCommon(Db_order_common order_common)
        {
            return db_w.Insert<Db_order_common>(order_common);
        }
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public int AddOrderGoods(Db_order_goods order_goods)
        {
            return db_w.Insert<Db_order_goods>(order_goods);
        }
        public int AddOrderPays(Db_order_pay order_pay)
        {
            return db_w.Insert<Db_order_pay>(order_pay);
        }
        public Db_goods SelectGoodsByIds(string ids)
        {
            string[] sArray = Regex.Split(ids, ",", RegexOptions.IgnoreCase);
            if (sArray.Length > 0)
            {
                string mid = sArray[(new Random()).Next(0, 99999) % sArray.Length];
                List<Db_goods> list = db_w.From<Db_goods>().Where(Db_goods._.goods_id == mid).ToList<Db_goods>();
                if (list.Count > 0) return list[0];
                else return null;
            }
            else return null;
        }
        public Db_member SelectBuyerById(string id)
        {
            List<Db_member> list = db_w.From<Db_member>().Where(Db_member._.member_id == id).ToList<Db_member>();
            if (list.Count > 0) return list[0];
            else return null;
        }
        public Db_shops SelectRandShop()
        {
            List<Db_shops> list = db_w.From<Db_shops>().Where(Db_shops._.CommentCount == 0 && Db_shops._.Status == 2).Top(9999).ToList<Db_shops>();
            if (list.Count > 0)
            {
                return list[(new Random()).Next(0, 99999) % list.Count];
            }
            else return null;
        }
        public List<Db_shops> SelectShopsByIDs(string shops)
        {
            string[] sArray = Regex.Split(shops, ",", RegexOptions.IgnoreCase);
            int[] ids = new int[sArray.Length];
            for (var i=0;i< sArray.Length;i++)
            {
                try
                {
                    ids[i] = Convert.ToInt32(sArray[i]);
                }
                catch { }
            }
            return db_w.From<Db_shops>().Where(Db_shops._.Id.SelectIn(ids)).ToList<Db_shops>();
        }
        public decimal SelectTotalPrice(string b_time, string e_time)
        {
            string sum = db_w.Sum<Db_order>(Db_order._.order_amount, Db_order._.add_time >= b_time && Db_order._.add_time <= e_time).ToString();
            decimal reVal = 0;
            try { reVal = Convert.ToDecimal(sum); }
            catch { reVal = 0; }
            return reVal;
        }
        public decimal SelectTotalPay(string b_time, string e_time)
        {
            string sum = db_w.Sum<Db_order>(Db_order._.order_amount, Db_order._.add_time >= b_time && Db_order._.add_time <= e_time && Db_order._.type_status==2).ToString();
            decimal reVal = 0;
            try { reVal = Convert.ToDecimal(sum); }
            catch { reVal = 0; }
            return reVal;
        }
        
    }
    
}
